package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemMessages;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SystemMessagesMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SystemMessagesQueryObject;
import cn.wolfcode.trip.base.service.ISystemMessagesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SystemMessagesServiceImpl implements ISystemMessagesService {
    @Autowired
    private SystemMessagesMapper systemMessagesMapper;
    @Override
    public PageInfo query(SystemMessagesQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<SystemMessages> list = systemMessagesMapper.query(qo);
        return new PageInfo(list);

    }

    @Override
    public void save(SystemMessages systemMessages, Long[] ids) {
        Date createTime = new Date();
        User user = new User();
        systemMessages.setAuthor(user);
        for(Long id:ids){
            systemMessages.getAuthor().setId(id);
            systemMessages.setCreateTime(createTime);
            systemMessages.setState(SystemMessages.STATE_UNREAD);
            systemMessagesMapper.insert(systemMessages);
        }

    }

    @Override
    public void delete(Long id) {
        systemMessagesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateAllState(Long userId) {
        systemMessagesMapper.updateAllState(userId);
    }
}
